#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 初始化表格信息
    // 首先创建三个 行/列
    ui->tableWidget->insertRow(0);
    ui->tableWidget->insertRow(1);
    ui->tableWidget->insertRow(2);

    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(1);
    ui->tableWidget->insertColumn(2);
    // 设置行和列的表头
    ui->tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("学号"));
    ui->tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("姓名"));
    ui->tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("班级"));

    ui->tableWidget->setVerticalHeaderItem(0, new QTableWidgetItem("1"));
    ui->tableWidget->setVerticalHeaderItem(1, new QTableWidgetItem("2"));
    ui->tableWidget->setVerticalHeaderItem(2, new QTableWidgetItem("3"));
    // 设置初始表项的信息
    ui->tableWidget->setItem(0,0,new QTableWidgetItem("1001"));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("张三"));
    ui->tableWidget->setItem(0,2,new QTableWidgetItem("电信一班"));

    ui->tableWidget->setItem(1,0,new QTableWidgetItem("1002"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("李四"));
    ui->tableWidget->setItem(1,2,new QTableWidgetItem("电信二班"));

    ui->tableWidget->setItem(2,0,new QTableWidgetItem("1003"));
    ui->tableWidget->setItem(2,1,new QTableWidgetItem("王五"));
    ui->tableWidget->setItem(2,2,new QTableWidgetItem("电信五班"));
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_rowAdd_clicked()
{
    // 新增一行
    // 首先获取有多少行了
    int rowCnt = ui->tableWidget->rowCount();
    // 获取输入框的表头信息
    const QString& s = ui->lineEdit->text();
    // 插入一行
    ui->tableWidget->insertRow(rowCnt);
    ui->tableWidget->setVerticalHeaderItem(rowCnt, new QTableWidgetItem(s));
}

void Widget::on_pushButton_rowDel_clicked()
{
    // 删除一行
    // 删除的是鼠标指定的行
    int row = ui->tableWidget->currentRow();
    ui->tableWidget->removeRow(row);
}

void Widget::on_pushButton_colAdd_clicked()
{
    // 新增一列
    // 首先获取有多少列了
    int colCnt = ui->tableWidget->columnCount();
    // 获取输入框的表头信息
    const QString& s = ui->lineEdit->text();
    // 插入一列
    ui->tableWidget->insertRow(colCnt);
    ui->tableWidget->setVerticalHeaderItem(colCnt, new QTableWidgetItem(s));
}

void Widget::on_pushButton_colDel_clicked()
{
    // 删除一列
    // 删除的是鼠标指定的列
    int col = ui->tableWidget->currentColumn();
    ui->tableWidget->removeColumn(col);
}
